package servlet;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Participation;
import model.Quest;
import model.Study;
import DAO.AnswerDao;
import DAO.DepositDao;
import DAO.ParticipationDao;
import DAO.QuestDao;
import DAO.StudyDao;
import DAO.StudyFineDao;
import DAO.UserDao;
import DAO.UserDetailDao;

public class QuestNewAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Quest quest = new Quest();
		QuestDao questDao = new QuestDao();
		
		HttpSession session=request.getSession();
		UserDetailDao userDetailDao = new UserDetailDao();
		ActionForward forward=new ActionForward();
		
			
		int studyId = request.getParameter("studyId") != null
				&& request.getParameter("studyId") != "" ? Integer
				.parseInt(request.getParameter("studyId")) : 0;
		int userId = session.getAttribute("userid") != null
				&& session.getAttribute("userid") != "" ? Integer
				.parseInt((String)session.getAttribute("userid")) : 0;
		System.out.println(userId +" " + studyId);
		if (userId < 1) {
			session.invalidate();
			Util.ShowError(response, "로그인해주세요");
		}
		if (studyId < 1) {
			Util.ShowError(response, "잘못 된 접근입니다");
		}
		request.setAttribute("studyId", studyId);	
		
		String title = request.getParameter("title")!= null && request.getParameter("title") 
				!= ""? request.getParameter("title") : null;	
		String content = request.getParameter("content")!= null && request.getParameter("content") 
				!= ""? request.getParameter("content") : null;		
		Date expiredTime = request.getParameter("expiredtime")!= null&& request.getParameter("expiredTime") 
				!= "" ? Date.valueOf(request.getParameter("expiredtime")) : null;
		
		Timestamp expiredTimeStamp = new Timestamp(expiredTime.getTime());
				
				
	
		
		if(content==null || expiredTime==null || title==null)  {
			//error return
			Util.ShowError(response, "값을 올바로 입력해주세요");
			
			return null;
		}
		
		title=Util.toUTF8(title);
		content=Util.toUTF8(content);
				
		quest.setSid(studyId);
		quest.setUid(userId);
		quest.setTitle(title);
		quest.setContent(content);
		quest.setExpiredTime(expiredTimeStamp);
		
		StudyFineDao studyFineDao = new StudyFineDao();
		ParticipationDao participationDao = new ParticipationDao();
		ArrayList <Participation> participationList = participationDao.getSameStudyMember(studyId);
	
		System.out.println(participationList.size());
		
		questDao.insertQuestInfo(quest);
		userDetailDao.updateTotalQuest(studyId);
		
		for(int i=0; i<participationList.size();i++){
			int userIdtemp = participationList.get(i).getUid();
			studyFineDao.updateTotalQuest(studyId, userIdtemp);
		}
		
	
		
	
		// go to details studyId;
		
		forward.setPath("./study_quest.jsp");

		return forward;
	}

}
